<?php

import('classes.handler.Handler');
class SSTMTGHandler extends Handler {
    function SS_TMTG($args, &$request) {
		
		$journal =& Request::getJournal();
		$journalid =$journal->getId();;

		$xThang1  = Request::getUserVar("dateExpire1Month");
		if ($xThang1)
		{
			$gcNam  = Request::getUserVar("dateExpire2Year");
			$gcNam2  = Request::getUserVar("dateExpire2Year");
			if ($gcNam !=$gcNam2)
			{
				echo '<script type="text/javascript">alert("Chi bao cao trong khoang thoi gian 1 nam"); </script>';
				return;
			}

			$xNgay1  = Request::getUserVar("dateExpire1Day");
			$xThang1  = Request::getUserVar("dateExpire1Month");
			$xNgay2  = Request::getUserVar("dateExpire2Day");
			$xThang2  = Request::getUserVar("dateExpire2Month");
			$date1 = mktime(0, 0, 0, $xThang1, $xNgay1, $gcNam);
			$date2 = mktime(0, 0, 0, $xThang2, $xNgay2, $gcNam);
			$ngay1 = date ( 'Y-m-d', $date1 );
			$ngay2 = date ( 'Y-m-d', $date2 );
		}
		else
		{
			return;
		}
		$matentk = explode("-", Request::getUserVar("matk"));
		$matk  = $matentk[0];
		$tentk = $matentk[1];

		$mant = Request::getUserVar("mant");
        $user =& Request::getUser();
        $userId = $user->getId();

        // lay so lieu
        $ctktDao =& DAORegistry::getDAO('CTKTDAO');
		$dict = NewDataDictionary($ctktDao->_dataSource);
		// Make sure the table exists
		$tables = $dict->MetaTables('TABLES', false);
		$tablename = "ctkt".$journalid. $gcNam;
		if (!in_array($tablename,$tables)) 
		{
			echo '<script type="text/javascript">alert("Khong co du lieu!"); window.close();</script>'; 
			//$oldurl= Request::url(null, "index", "SS_TMTG"); 
			//Request::redirectUrl($oldurl);

		}
		$tablename = "sdtt".$journalid. $gcNam;
		if (!in_array($tablename,$tables)) 
		{
			echo '<script type="text/javascript">alert("Chua cap nhat so du dau nam!"); window.close();</script>';
		}
	    $ctkts =& $ctktDao->getCTKT_BCTMTG($journalid, $gcNam, $ngay1, $ngay2, $matk, $mant);

		$psdks =& $ctktDao->getCTKT_BCTMTG_PSDK($journalid, $gcNam, $ngay1, $ngay2, $matk, $mant);

		$sodudks =& $ctktDao->getCTKT_BCTMTG_SDDK($journalid, $gcNam, $ngay1, $ngay2, $matk, $mant);

		$dauky = $sodudks["dknovn"] + $psdks["psnovn"] - $psdks["pscovn"] ;
		$daukynt = $sodudks["dknont"] + $psdks["psnont"] - $psdks["pscont"] ;

		//parent::ManagerHandler();
		$templateMgr =& TemplateManager::getManager();
		$templateMgr->assign('tentk',$tentk);
        $templateMgr->assign('tbdau',$ngay1);
        $templateMgr->assign('tkthuc',$ngay2);
		$templateMgr->addJavaScript("classes/acct/js/soquy.js");

        $templateMgr->assign('TBC', "So TMTG" );
        $templateMgr->assign_by_ref('ctkts', $ctkts);

		$templateMgr->assign('dauky', $dauky);
		$templateMgr->assign('cuoiky', $sdck);
        $templateMgr->assign('tencty','Công ty dược TW3');
        $templateMgr->assign('diachi','Địa chỉ: Đà Nẵng');
		$templateMgr->assign('mant',$mant);

        $templateMgr->assign('loaitk',$matk);
		if (substr($matk,0,3) == "111")
		{
			if ($mant == "VND")
			{
		        $templateMgr->assign('tieude','SỔ QUỸ TIỀN MẶT');
		        $templateMgr->display('acctSS/soquy.tpl');
			}
			else
			{
		        $templateMgr->assign('tieude','SỔ QUỸ TIỀN MẶT '.$mant);
		        $templateMgr->display('acctSS/soquynt.tpl');
			}
		}
		else
		{
			if ($mant == "VND")
			{
				$templateMgr->assign('tieude','SỔ THEO DÕI TIỀN GỬI ');
		        $templateMgr->display('acctSS/soquy.tpl');
			}
			else
			{
				$templateMgr->assign('tieude','SỔ THEO DÕI TIỀN GỬI '.$mant);
		        $templateMgr->display('acctSS/soquynt.tpl');
			}
		}
    }
}

?>
